package com.wcpdoc.exam.api.controller;

import com.wcpdoc.core.controller.BaseController;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.exception.MyException;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.exam.core.entity.MyAbnormal;
import com.wcpdoc.exam.core.service.MyAbnormalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 我的反馈控制层
 * 
 */
@Controller
@RequestMapping("/api/myAbnormal")
public class ApiMyAbnormalController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(ApiMyAbnormalController.class);
	
	@Resource
	private MyAbnormalService abnormalService;

	/**
	 * 我的反馈列表
	 * 
	 * @return pageOut
	 * type  1错题，2收藏
	 */
	@RequestMapping("/listpage")
	@ResponseBody
	public PageResult listpage(Integer type) {
		try {
//			if (!ValidateUtil.isValid(type)) {
//				throw new MyException("type error");
//			}
			PageIn pageIn = new PageIn(request);
			pageIn.addAttr("curUserId", getCurUser().getId());
			pageIn.addAttr("type", type);
//			pageIn.addAttr("questionTypeId", questionTypeId);
			PageOut pageOut = abnormalService.getListpage(pageIn);
			return PageResultEx.ok().data(pageOut);
		} catch (Exception e) {
			log.error("我的收藏列表错误：", e);
			return PageResult.err();
		}
	}

	/**
	 * 反馈添加
	 *
	 * @param myAbnormal
	 * @return PageResult
	 */
	@RequestMapping("/add")
	@ResponseBody
	public PageResult addAbnormal(MyAbnormal myAbnormal) {
		try {
			myAbnormal.setUserId(getCurUser().getId());
			myAbnormal.setUpdateTime(new Date());
			abnormalService.addAbnormal(myAbnormal);
			return PageResult.ok();
		} catch (MyException e) {
			log.error("添加错误：{}", e.getMessage());
			return PageResult.err().msg(e.getMessage());
		}  catch (Exception e) {
			log.error("添加错误：", e);
			return PageResult.err();
		}
	}

	/**
	 * 反馈删除
	 *
	 * @return pageOut
	 */
	@RequestMapping("/del")
	@ResponseBody
	public PageResult delAbnormal(Integer id) {
		try {
			if(!ValidateUtil.isValid(id)){
				throw new MyException("id error");
			}
			abnormalService.del(id);
			return PageResult.ok();
		} catch (MyException e) {
			log.error("删除错误：{}", e.getMessage());
			return PageResult.err().msg(e.getMessage());
		}  catch (Exception e) {
			log.error("删除错误：", e);
			return PageResult.err();
		}
	}

	/**
	 * 反馈类型列表
	 *
	 * @return pageOut
	 */
	@RequestMapping("/typeList")
	@ResponseBody
	public PageResult getAbnormalTypeList() {
		try {
			return PageResultEx.ok().data(abnormalService.getAbnormalTypeList());
		} catch (MyException e) {
			return PageResult.err().msg(e.getMessage());
		}  catch (Exception e) {
			return PageResult.err();
		}
	}

	/**
	 * 试题反馈类型列表
	 *
	 * @return pageOut
	 */
	@RequestMapping("/sysTypeList")
	@ResponseBody
	public PageResult getQuestionTypeList() {
		try {
			return PageResultEx.ok().data(abnormalService.getAbnormalSysTypeList());
		} catch (MyException e) {
			return PageResult.err().msg(e.getMessage());
		}  catch (Exception e) {
			return PageResult.err();
		}
	}
	
}
